new Vue({
	el: '#app',
	// 选项
	data: {
		uid: '',
		qid: '',
		u_id: '',
		qtitle: '',
		qcontent: '',
		qcreated_at: '',
		qnickname: '',
		qis_follow: '',
		count: '',
		isJump: '',
		answer: [],
		path: '',
		page: '1',
		t: [],
		a: [],
		isShow: false
	},
	mounted: function() {
		this.init();
	},
	methods: {
		init: function() {
			this.getQid();
		},
		//获得问题的id
		getQid: function() {
			var self = this;
			self.uid = $api.getStorage('uid');
			console.log(self.uid);
			if(self.uid == "" || self.uid == null) {
				self.isJump = false;
			} else {
				self.isJump = true;
			};
			self.getDetails();
		},
		getDetails: function() {
			var self = this;
			//self.seeQuestion();
			apiready = function() {
				var fid = api.pageParam;
				self.qid = fid.qid;
				console.log(fid,name);
				if(fid.name) {
					api.closeWin({
						name: fid.name
					});
				}
				console.log(self.qid);
				//loading
				self.isS();
				api.addEventListener({
					name: 'scrolltobottom',
					extra: {
						threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
					}
				}, function(ret, err) {
					//alert('已滚动到底部');
					self.page++;
					if(self.page > self.pageCount) {
						api.toast({
							msg: '没有更多了',
							duration: 1000,
							location: 'bottom'
						});
					} else {
						self.seeQuestion();
					}
				});
			};
		},
		isS: function() {
			var self = this;
			var UILoading = api.require('UILoading');
			if(self.isShow) {
				console.log(self.loadingId);
				UILoading.closeFlower({
					id: self.loadingId
				});
			} else {
				UILoading.flower({
					center: {
						x: api.winWidth / 2.0,
						y: api.winHeight / 2.0
					},
					size: 30,
					fixed: true
				}, function(ret) {
					console.log(JSON.stringify(ret));
					self.loadingId = ret.id;
				});
				self.seeQuestion();
			}
		},
		//返回上一页
		backone:function(){
			api.closeWin();
		},
		//获得问题详情数据
		seeQuestion: function() {
			var self = this;
			console.log(self.uid);
			var data = {
				id: self.qid,
				user_id: self.uid,
				page: self.page
			};
			$.post(base + config.sns_question_view, data, function(msg) {
				console.log(msg);
				self.qtitle = msg.data.title;
				self.u_id = msg.data.user_id;
				self.qcontent = msg.data.content;
				if(msg.data.created_at) {
					self.qcreated_at = msg.data.created_at;
				} else {
					self.qcreated_at = "0 0"
				}
				self.qnickname = msg.data.nickname;
				self.qis_follow = msg.data.is_follow;
				self.count = msg.answer.count;
				self.pageCount = msg.answer.page_count;
				self.t = msg.answer.data;
				for(var i = 0; i < self.t.length; i++) {
					self.answer.push(self.t[i]);
				};
				self.path = msg.url;
				self.isShow = true;
				self.isS();
			}, 'json');

		},
		//跳转到编辑问题页面
		jumpEdit: function() {
			var self = this;
			api.openWin({
				name: 'editQuestion',
				url: 'editQuestion.html',
				//vScrollBarEnabled: false,
				//reload: true,
				pageParam: {
					qid: self.qid
				}
			});
		},
		//跳转回答问题页面
		jump: function() {
			var self = this;
			if(self.uid) {
				console.log(self.qid);
				api.openWin({
					name: 'reply-topic',
					url: 'replyTopic.html',
					vScrollBarEnabled: false,
					//					reload: true,
					pageParam: {
						qid: self.qid
					}
				});
			} else {
				api.confirm({
					title: '尚未登录',
					msg: '是否前往登录页面',
					buttons: ['确定', '取消']
				}, function(ret, err) {
					var index = ret.buttonIndex;
					if(index == 1) {
						self.layout();
					}
				});
			}
		},
		//跳转到评论列表
		jumpList: function(id) {
			api.openWin({
				name: 'reply-comment',
				url: 'replyComment.html',
				vScrollBarEnabled: false,
				//				reload: true,
				pageParam: {
					aid: id
				}
			});
		},
		//点赞
		agree: function(id) {
			var self = this;
			console.log(self.uid);
			if(self.uid == "" || self.uid == null) {
				api.confirm({
					title: '尚未登录',
					msg: '是否前往登录页面',
					buttons: ['确定', '取消']
				}, function(ret, err) {
					var index = ret.buttonIndex;
					if(index == 1) {
						self.layout();
					}
				});
			} else {
				var data = {
					id: id,
					user_id: self.uid
				}
				$.post(base + config.sns_answer_like, data, function(msg) {
					console.log(msg);
					if(msg.error == 0) {
						self.page = "1";
						self.answer = [];
						self.seeQuestion();
					} else {
						console.log(msg.error_msg);
						api.toast({
							msg: msg.error_msg,
							duration: 2000,
							location: 'middle'
						});
					}
				}, 'json');
			}
		},
		//关注
		follow: function() {
			var self = this;
			if(self.uid == "" || self.uid == null) {
				api.confirm({
					title: '尚未登录',
					msg: '是否前往登录页面',
					buttons: ['确定', '取消']
				}, function(ret, err) {
					var index = ret.buttonIndex;
					if(index == 1) {
						self.layout();
					} else {

					}
				});
			} else {
				var data = {
					sns_question_id: self.qid,
					user_id: self.uid
				}
				$.post(base + config.sns_question_follow_add, data, function(msg) {
					console.log(msg);
					if(msg.error == 0) {
						self.seeQuestion();
					} else {
						console.log(msg.error_msg);
						api.toast({
							msg: msg.error_msg,
							duration: 2000,
							location: 'middle'
						});
					}
				}, 'json');
			}

		},
		//登出
		layout: function() {
			var self = this;
			api.openWin({
				name: 'login',
				url: '../../html/login/login.html',
				reload: true,
				pageParam: {
					openName: 'question-details',
					qid: self.qid,
					type: 1
				}
			});
		}
	}
})